package com.lanqiao.controller;

import com.lanqiao.domain.House;
import com.lanqiao.domain.UsersInfo;
import com.lanqiao.service.IUserInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * @author 张建平 on 2020/8/11.
 */
@Controller
@RequestMapping("/user")
@SessionAttributes({"logUsersInfo","condition"})  // 将Model中的属性同步到session当中
public class UserController { // controller -> [Service业务逻辑层]-> DAO Mapper(insert ,update) -> mybatis

    static Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    IUserInfoService userInfoService; // bean id: @Service("userInfoService")

    /**
     * @param user
     * @param modelMap 为请求作用域
     * @return
     */
    @RequestMapping("/login")  // 请求路径：/user/login
    public String login(UsersInfo user, ModelMap modelMap) {

        logger.debug("login....");
        logger.debug(user);

        UsersInfo logUsersInfo = this.userInfoService.selectForLogin(user);
        // 登录成功, 保存到 HttpSession
        if (logUsersInfo != null) {
            modelMap.addAttribute("logUsersInfo",logUsersInfo);
            modelMap.addAttribute("condition", new House());
        } else {
            //  登录失败
            return "redirect:/login_form.jsp";
        }
        /*
         只要返回的字符串带有前缀：forward: 或 redirect: 视图解析器失效（不解析）
         所以此时返回的字符必须是一个从WEB ROOT开始的一个完整路径
         return "forward:/views/test.jsp";                    //转发到页面
         return "forward:/user/login";                        //转发到控制器方法
         return "redirect:/views/test.jsp?paramName=james";   //重定向到页
         return "redirect:/user/login?paramName=james";       //重定向的控制器的方法
        */
        return "redirect:/house/search";  // 重定向到HouseController.search()
    }


    //@RequestMapping("/reg")  //  /user/reg

    // 当请求中包含有指定参数且值等于abc时通过此方法处理请求,如：http://localhost:8080/zf/abasdfs?xxx=abc
    @RequestMapping(params = {"xxx=abc"})
    public ModelAndView reg() {
         /*
            ModelAndView包含了视图要显示的模型数据，同时学包含了视图的相关信息
         */
        ModelAndView mav = new ModelAndView();
        mav.setViewName("example"); // /views/example.jsp
        mav.addObject("myName", "james");
        mav.addObject("myList", Arrays.asList(1,4,6,8,9));

        return mav;
    }


    @RequestMapping("/add")  //  /user/add
    public String add() {

        return "";
    }



}
